﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace BLL.Dal
{

    /// <summary>
    /// 数据访问类:AttachmentDAL
    /// </summary>
    public partial class AttachmentDAL
    {
        public AttachmentDAL()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid attGuid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbAttachment");
            strSql.Append(" where attGuid=@attGuid ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@attGuid", OleDbType.Guid)			};
            parameters[0].Value = attGuid;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Attachment model)
        {
            ///设备创建时间
            model.attGuid = Guid.NewGuid();
            model.createdDateTime = DateTime.Now;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbAttachment(");
            strSql.Append("attGuid,refTable,refKey,attTitle,attType,createdDateTime,filePath,fileExten)");
            strSql.Append(" values (");
            strSql.Append("@attGuid,@refTable,@refKey,@attTitle,@attType,@createdDateTime,@filePath,@fileExten)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@attGuid", OleDbType.Guid),
					new OleDbParameter("@refTable", OleDbType.VarChar,255),
					new OleDbParameter("@refKey", OleDbType.VarChar,255),
					new OleDbParameter("@attTitle", OleDbType.VarChar,255),
					new OleDbParameter("@attType", OleDbType.VarChar,255),
					new OleDbParameter("@createdDateTime", OleDbType.Date),
					new OleDbParameter("@filePath", OleDbType.VarChar,255),
					new OleDbParameter("@fileExten", OleDbType.VarChar,255)};
            parameters[0].Value = Guid.NewGuid();
            parameters[1].Value = model.refTable;
            parameters[2].Value = model.refKey;
            parameters[3].Value = model.attTitle;
            parameters[4].Value = model.attType;
            parameters[5].Value = model.createdDateTime;
            parameters[6].Value = model.filePath;
            parameters[7].Value = model.fileExten;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Attachment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbAttachment set ");
            strSql.Append("attGuid=@attGuid,");
            strSql.Append("refTable=@refTable,");
            strSql.Append("refKey=@refKey,");
            strSql.Append("attTitle=@attTitle,");
            strSql.Append("attType=@attType,");
            strSql.Append("createdDateTime=@createdDateTime,");
            strSql.Append("filePath=@filePath,");
            strSql.Append("fileExten=@fileExten");
            strSql.Append(" where attGuid=@attGuid ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@attGuid", OleDbType.Guid),
					new OleDbParameter("@refTable", OleDbType.VarChar,255),
					new OleDbParameter("@refKey", OleDbType.VarChar,255),
					new OleDbParameter("@attTitle", OleDbType.VarChar,255),
					new OleDbParameter("@attType", OleDbType.VarChar,255),
					new OleDbParameter("@createdDateTime", OleDbType.Date),
					new OleDbParameter("@filePath", OleDbType.VarChar,255),
					new OleDbParameter("@fileExten", OleDbType.VarChar,255)};
            parameters[0].Value = model.attGuid;
            parameters[1].Value = model.refTable;
            parameters[2].Value = model.refKey;
            parameters[3].Value = model.attTitle;
            parameters[4].Value = model.attType;
            parameters[5].Value = model.createdDateTime;
            parameters[6].Value = model.filePath;
            parameters[7].Value = model.fileExten;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid attGuid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbAttachment ");
            strSql.Append(" where attGuid=@attGuid ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@attGuid", OleDbType.Guid)			};
            parameters[0].Value = attGuid;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string attGuidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbAttachment ");
            strSql.Append(" where attGuid in (" + attGuidlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Attachment GetModel(Guid attGuid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select attGuid,refTable,refKey,attTitle,attType,createdDateTime,filePath,fileExten from tbAttachment ");
            strSql.Append(" where attGuid=@attGuid ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@attGuid", OleDbType.Guid)			};
            parameters[0].Value = attGuid;

            Model.Attachment model = new Model.Attachment();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Attachment DataRowToModel(DataRow row)
        {
            Model.Attachment model = new Model.Attachment();
            if (row != null)
            {
                if (row["attGuid"] != null && row["attGuid"].ToString() != "")
                {
                    model.attGuid = new Guid(row["attGuid"].ToString());
                }
                if (row["refTable"] != null)
                {
                    model.refTable = row["refTable"].ToString();
                }
                if (row["refKey"] != null)
                {
                    model.refKey = row["refKey"].ToString();
                }
                if (row["attTitle"] != null)
                {
                    model.attTitle = row["attTitle"].ToString();
                }
                if (row["attType"] != null)
                {
                    model.attType = row["attType"].ToString();
                }
                if (row["createdDateTime"] != null && row["createdDateTime"].ToString() != "")
                {
                    model.createdDateTime = DateTime.Parse(row["createdDateTime"].ToString());
                }
                if (row["filePath"] != null)
                {
                    model.filePath = row["filePath"].ToString();
                }
                if (row["fileExten"] != null)
                {
                    model.fileExten = row["fileExten"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select attGuid,refTable,refKey,attTitle,attType,createdDateTime,filePath,fileExten ");
            strSql.Append(" FROM tbAttachment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbAttachment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.attGuid desc");
            }
            strSql.Append(")AS Row, T.*  from tbAttachment T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbAttachment";
            parameters[1].Value = "attGuid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }

}
